//index.js
//获取应用实例
const app = getApp()
import {chuangyeList, rmFuwuLists, rmhdList, publicList} from "../../utils/data/tabbar";
import { getServiceType } from  "../../utils/api/module/cyfw";
// import { allColumn} from "../../utils/api/module/lanmu.js";
import { yuanquList } from  "../../utils/api/module/yuanqu.js";
import {articleList} from "../../utils/api/module/wenzhang";
import  {getZzList} from '../../utils/api/module/task.js'

Page({
    data: {
        gbConfig: getApp().globalData.config,
        // 搜索
        searchs: {
            icon: '../../assets/img/search_01.png',
            searchVal: '',
            searchPL: "请输入搜索内容"
        },
        // 轮播图
        lunbotu:{
            background: [
                { pic: '/uploads/202101/07/202101070001395787.png'},
            ],
            indicatorDots: false,
            vertical: false,
            autoplay: true,
            interval: 3000,
            duration: 600,
            circular: true,
            swiperCurrent: 0
        },
        publicList,
        chuangyeList,   //创业服务
        hotyqList:'',  //热门园区
        // rmFuwuLists, //热门服务
        rmFuwuLists: [],
        jpzzList:'', //精品招租
        rmzcList:'',   //热门活动
        rmhdList,  //热门活动
        defaImg: getApp().defaultImg,
        activityData: [],
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../enterprise/enterprise'
        })
    },
    onLoad: function () {
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
        this.getYqList();
        this.getArticleList();
        this.getmyrentList();
        this.getServiceType();
        let data = [];
        getApp().activityData.map(item => {
            item.map(items => {
                if(data.length <= 4) data.push(items);
            });
        });
        this.setData({ activityData: data });
    },
    // 筛选数据
    getServiceType: function() {
        getServiceType({
            parent: 0,
            type: 'allchild'
        }).then(res => {
            this.data.selectData = res.data.data;
            this.setData({ rmFuwuLists: this.data.selectData })
        });
    },
    getUserInfo: function (e) {
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    /**
     * l轮播图current设置
     * @param e 参数
     */
    swiperAnimationListener(e){
        this.setData({
            ["lunbotu.swiperCurrent"]: e.detail.current
        })
    },
    /**
     * 创业园区点击事件
     * @param e 参数
     */
    cyyqClickItem(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.path,
        })
    },
    // 页面跳转
    go: function(e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({
          url: url,
        })
    },
    /**
     * 获取首页热门园区列表
     */
    getYqList(){
        yuanquList({
            "limit": "4"
        }).then(res => {
            this.setData({
                hotyqList: res.data.data,
                ["lunbotu.background"]: [...this.data.lunbotu.background,...res.data.data.slice(0,2)]
            })
        })
    },
    getArticleList(){
        articleList({
            "columnid": 2,
            "limit":3
        }).then(res => {
            this.setData({
                rmzcList:  res.data.data,
            })
        })
    },
    /**
     * 前往搜索页面
     */
    toSearch: function() {
        wx.navigateTo({
            url:'/pages/search/index/index'
        })
    },
    /**
     * 点击banner图片， 跳转到相应的详情
     * @param e
     */
    bannerClick(e){
        let id = e.currentTarget.dataset.id;
        /*判断banner是否有字段id, */
        if(id != undefined){
            wx.navigateTo({
                url: "/pages/index/business/detail/index?id="+id,
            })
        }
    },
    /**
     * 获取精品招租的列表数据
     * @return {[type]} [description]
     */
    getmyrentList(){
        getZzList({
            isrec:1,
            limit: 5,
        }).then(res => {
            this.setData({
                jpzzList: res.data.data,
            })
        })
    },
    // 热门活动
    bindList: function(e) {
        let userData = wx.getStorageSync('userInfoData1'),
            url = '/pages/enterprise/authentication/index',
            title = '需认证通过才能使用该功能',
            btnContent = '重新认证',
            id = e.currentTarget.dataset.id;
        if(userData.data && userData.data.certstatus == 3) {
            if(!userData.userId36) {
                wx.showModal({
                    title: '提示',
                    content: '登录异常，请重新登录！',
                    cancelText: '取消登录',
                    cancelColor: '#999999',
                    confirmText: '重新登录',
                    confirmColor: '#3b99f5',
                    success: res => {
                    //   this.data.theRedEnvelopeSuper = false;
                      if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/loginAndRegister/login/index'
                        })
                      }
                    }
                });
                return false;
            };
            wx.navigateToMiniProgram({
                appId: 'wx6f6831e5b83b7535',//互动吧小程序appid
                path:'/pages/detail/detail?id=' + id,//跳转地址
                extraData:{//用户信息参数，根据自己代码自行赋值
                    "userId36": userData.userId36,
                    "userSecret":  userData.userSecret,
                    "userSign":  userData.userSign
                    },
                envVersion: 'release', // 线上
            });
            return false;
        };
        if(!userData.data || !userData.data.certstatus && userData.data.certstatus != 0) {
            url = '/pages/loginAndRegister/login/index';
            title = '必须登录且认证通过才能使用该功能';
            btnContent = '去登录'
        };
        if(userData.data && userData.data.certstatus == 0) {
            url = '/pages/enterprise/authentication/index';
            title = '需认证通过才能使用该功能';
            btnContent = "去认证";
        };
        if(userData.data && userData.data.certstatus == 1) {
            url = '/pages/enterprise/authentication/index';
            title = '需认证通过才能使用该功能';
            btnContent = "去查看";
        };
        wx.showModal({
            title: '提示',
            content: title,
            cancelText: '取消',
            cancelColor: '#999999',
            confirmText: btnContent,
            confirmColor: '#3b99f5',
            success: res => {
              this.data.theRedEnvelopeSuper = false;
              if (res.confirm) {
               wx.navigateTo({
                 url: url
               })
              }
            }
        });
    }
})
